﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

using FbKPI.Model;
namespace FbKPI.Controller
{
    public class sysLogController : FbKPI.Controller.dbConnection.ConnectionClass
    {
        public string Save(sysLog sysLog)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_SysLog_Insert";

            command.Parameters.Add("@Description", SqlDbType.VarChar).Value = sysLog.Description;
            command.Parameters.Add("@Source", SqlDbType.VarChar).Value = sysLog.Source;
            command.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = sysLog.CreatedDate;
            command.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = sysLog.CreatedBy;
            command.Parameters.Add("@LogEvent", SqlDbType.VarChar).Value = sysLog.LogEvent;
           
            

            return ExecuteNonQuery(command);
        }

        public string Delete(int LogID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_SysLog_Delete";
            command.Parameters.Add("@LogID", SqlDbType.Int).Value = LogID;
           

            return ExecuteNonQuery(command);
        }

       

        public IList<sysLog> GetAll(string sSearch, int PageNumber, int Datalength, string SortCol, string SortDirection, out int TotalData, int BusinessGroupID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_SysLog_SelectAll";
            command.Parameters.Add("@SEARCH", SqlDbType.VarChar).Value = sSearch;
           

            DataSet dx = GetTable(command);

            List<sysLog> sysLog = new List<sysLog>();            

            switch(SortDirection)
            {
                case "asc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description ASC";
                            break;
                        case "Source":
                            dx.Tables[0].DefaultView.Sort = "Source ASC";
                            break;
                        case "CreatedDate":
                            dx.Tables[0].DefaultView.Sort = "CreatedDate ASC";
                            break;
                        case "CreatedBy":
                            dx.Tables[0].DefaultView.Sort = "CreatedBy ASC";
                            break;
                        case "LogEvent":
                            dx.Tables[0].DefaultView.Sort = "LogEvent ASC";
                            break;
                      
                    }
                    break;
                case "desc":
                    switch (SortCol)
                    {
                        case "Description":
                            dx.Tables[0].DefaultView.Sort = "Description DESC";
                            break;
                        case "Source":
                            dx.Tables[0].DefaultView.Sort = "Source DESC";
                            break;
                        case "CreatedDate":
                            dx.Tables[0].DefaultView.Sort = "CreatedDate DESC";
                            break;
                        case "CreatedBy":
                            dx.Tables[0].DefaultView.Sort = "CreatedBy DESC";
                            break;
                        case "LogEvent":
                            dx.Tables[0].DefaultView.Sort = "LogEvent DESC";
                            break;
                    }
                    break;
            }

            DataTable dt = dx.Tables[0].DefaultView.ToTable();

            if (dt.Rows.Count > 0)
            {
                for (int i = PageNumber; i < Datalength; i++)
                {
                    if (i < dt.Rows.Count)
                    {
                        sysLog p = new sysLog();
                        p.LogID = Convert.ToInt16(dt.Rows[i]["LogID"].ToString());
                        p.Description = dt.Rows[i]["Description"].ToString();
                        p.Source = dt.Rows[i]["Source"].ToString();
                        p.CreatedBy = dt.Rows[i]["CreatedBy"].ToString();
                        p.CreatedDate = dt.Rows[i]["CreatedDate"] == DBNull.Value ? DateTime.Now : Convert.ToDateTime(dt.Rows[i]["CreatedDate"].ToString());
                        p.LogEvent = dt.Rows[i]["LogEvent"].ToString();
                        sysLog.Add(p);
                    }
                    else
                    {
                        break;
                    }

                }
            }

            TotalData = dt.Rows.Count;

            return sysLog.ToList();
        }

        public sysLog GetByID(int ID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_SysLog_GetByID";
            command.Parameters.Add("@LogID", SqlDbType.Int).Value = ID;

            IList<string> Data = GetSingleRow(command);
            sysLog sysLog = null;

            if (Data != null && Data.Count > 0)
            {
                sysLog = new sysLog();
                sysLog.LogID = Convert.ToInt16(Data[0]);
                sysLog.Description = Data[1].ToString();
                sysLog.Source = Data[2].ToString();
                sysLog.CreatedDate = Data[3] == string.Empty ? DateTime.Now: Convert.ToDateTime(Data[3].ToString());
                sysLog.CreatedBy = Data[4].ToString();
                sysLog.LogEvent = Data[5].ToString();
            }

            return sysLog;
        }

       
    }
}